from django.db import models
from users.models import UserModel,User_Coupon,Address
from shops.models import Dishes,Shops


# 订单表       （结算才会有订单，订单信息）
class Order(models.Model):
    STATUS_CHOICE=(
        ("0","待支付"),
        ("1","待发货"),
        ("2","待收货"),
        ("3","待评价"),
        ("4","已完成")
    )
    order_id = models.CharField("订单编号",max_length=100,primary_key=True)
    user = models.ForeignKey(UserModel,on_delete=models.CASCADE,verbose_name="用户")
    addr = models.ForeignKey(Address,on_delete=models.CASCADE,verbose_name="收货地址")
    shop = models.ForeignKey(Shops,on_delete=models.CASCADE,verbose_name='商家',null=True)
    total_amount = models.DecimalField("总价格",max_digits=5,decimal_places=2)
    total_count = models.IntegerField("总商品数",default=1)
    freight = models.DecimalField("运费",max_digits=5,decimal_places=2)
    pay_method = models.CharField("支付方式",max_length=10,choices=(("1","支付宝"),("2","银联")),default="1")
    pay_status = models.CharField("支付状态",max_length=10,choices=STATUS_CHOICE)
    created_time = models.DateTimeField("创建时间",auto_now_add=True)
    updated_time = models.DateTimeField("更新时间",auto_now=True)
    class Meta:
        db_table="order_tb"
        verbose_name_plural="订单表"
    def __str__(self):
        return "用户的订单号%s"%self.order_id



# 订单商品表，购物车      （待购买的每个商品信息）
class Shopping_trolley(models.Model):
    dishes = models.ForeignKey(Dishes,on_delete=models.CASCADE,verbose_name="菜品id")
    shops = models.ForeignKey(Shops,on_delete=models.CASCADE,verbose_name='商家id')
    user = models.ForeignKey(UserModel,on_delete=models.CASCADE,verbose_name='用户id')
    dishes_count = models.IntegerField("购买数量",default=1)
    dishes_price = models.DecimalField("菜品单价",max_digits=5,decimal_places=2)
    is_coupon = models.ForeignKey(User_Coupon,on_delete=models.CASCADE,verbose_name='用户的优惠券')
    class Meta:
        db_table="shopping_trolley_tb"
        verbose_name_plural="订单购物车表"
    def __str__(self):
        return "订单的购物车菜品id%s"%self.dishes



class OrderGoods(models.Model):
    """订单商品"""

    dishes = models.ForeignKey(Dishes, on_delete=models.CASCADE, verbose_name="菜品")
    count = models.IntegerField(default=1, verbose_name="数量")
    price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name="单价")
    comment = models.TextField(default="", verbose_name="评价信息")
    order = models.ForeignKey(Order, related_name='orderGood', on_delete=models.CASCADE, verbose_name="订单号")
    is_anonymous = models.BooleanField(default=False, verbose_name='是否匿名评价')
    is_commented = models.BooleanField(default=False, verbose_name='是否评价')

    class Meta:
        db_table = "order_goods_table"
        verbose_name_plural = '订单菜品表'

    def __str__(self):
        return self.dishes.id




# # # 评论
class Comment(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=Shops, on_delete=models.CASCADE)
    order = models.ForeignKey(verbose_name="订单", to=Order, on_delete=models.CASCADE, blank=True, null=True)
    content = models.CharField(verbose_name="评论内容", max_length=128,default='')
    is_reply = models.BooleanField(verbose_name="是否回复",null=True)
    comment_id = models.ForeignKey('Comment', on_delete=models.CASCADE, verbose_name='被回复的评论id', default=None,null=True)
    class Meta:
        db_table="comment_tb"
        verbose_name_plural="订单购物车表"
    def __str__(self):
        return "商家%s,用户"%(self.shops)


